package com.phpxiu.app.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;

import com.phpxiu.app.adapter.holder.SearchResultViewHolder;
import com.phpxiu.app.config.HttpConfig;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.MySelfInfo;
import com.phpxiu.app.model.event.OnRefreshFollowCount;
import com.phpxiu.app.model.list.SearchResultContent;
import com.phpxiu.app.okhttp.OKHttp;
import com.phpxiu.app.okhttp.OKHttpResponseModel;
import com.phpxiu.app.okhttp.handler.OKHttpUIHandler;
import com.phpxiu.app.okhttp.param.OKHttpParam;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by WuJinZhou on 2016/6/7.
 */
public class UserSearchListAdapter extends BaseAdapter<SearchResultContent> implements View.OnClickListener{
    public final static String TAG="UserSearchListAdapter";
    private OKHttpParam attentionParam;

    public UserSearchListAdapter(Context context, List<SearchResultContent> data) {
        super(context, data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SearchResultContent content=data.get(position);
        content.setPosition(position);
        SearchResultViewHolder holder=null;
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.user_search_result_list_item_view,null);
            holder=new SearchResultViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder=(SearchResultViewHolder)convertView.getTag();
        holder.setContent(content);
        holder.headView.setImageURI(Uri.parse(content.getAvatar()));
        holder.nickView.setText(content.getNickname());
        String sex=content.getSex();
        if("1".equals(sex)){
            holder.sexView.setImageResource(R.mipmap.user_sex_icon_male);
        }else{
            holder.sexView.setImageResource(R.mipmap.user_sex_icon_female);
        }
        if(!"".equals(content.getLvl())){
            holder.lv.setVisibility(View.VISIBLE);
            holder.lv.setImageURI(Uri.parse("asset:///lvIcons/" + content.getLvl() + ".png"));
        }else{
            holder.lv.setVisibility(View.INVISIBLE);
        }
        if(content.getIntro()!=null&&!"".equals(content.getIntro())){
            if(holder.introView.getVisibility()!=View.VISIBLE){
                holder.introView.setVisibility(View.VISIBLE);
            }
            holder.introView.setText(content.getIntro());
        }else {
            if(holder.introView.getVisibility()==View.VISIBLE){
                holder.introView.setVisibility(View.GONE);
            }
        }
        holder.checkBtn.setTag(content);
        holder.checkBtn.setOnClickListener(this);
        if(content.getRelationship()==2){
            holder.checkBtn.setImageResource(R.mipmap.list_relation_icon_2);
        }else if(content.getRelationship()==1){
            holder.checkBtn.setImageResource(R.mipmap.list_relation_icon_1);
        }else{
            holder.checkBtn.setImageResource(R.mipmap.list_relation_icon_0);
        }
        //是自己不做操作
        if(MySelfInfo.getInstance().getId().equals(content.getUid())){
            holder.checkBtn.setVisibility(View.INVISIBLE);
        }else {
            if(holder.checkBtn.getVisibility()!=View.VISIBLE){
                holder.checkBtn.setVisibility(View.VISIBLE);
            }
        }
        holder.showDivider();
        if(data.size()>0){
            if(position==data.size()-1){
                holder.hideDivider();
            }
        }
        return convertView;
    }

    @Override
    public void onClick(View v) {
        if(v.getTag()!=null&&v.getTag() instanceof SearchResultContent){
            int position=((SearchResultContent)v.getTag()).getPosition();
            SearchResultContent a=data.get(position);
            int r=a.getRelationship();
            if(r==1||r==2) {
                a.setRelationship(0);
                attentionChange(a.getUid(),true);
            }else{
                a.setRelationship(1);
                attentionChange(a.getUid(),false);
            }
            notifyDataSetChanged();
        }
    }

    /**
     * 更改关注状态
     */
    private void attentionChange(String uid,boolean isAttention){
        EventBus.getDefault().post(new OnRefreshFollowCount(!isAttention));
        if(attentionParam==null){
            attentionParam= OKHttpParam.builder(false);
        }
        attentionParam.put("uid",uid);
        String url=null;
        if(isAttention){
            url= HttpConfig.API_CANCEL_ATTENTION_USER;
        }else {
            url=HttpConfig.API_ATTENTION_USER;
        }
        OKHttp.post(url, attentionParam.jsonParam(), TAG, new OKHttpUIHandler(OKHttpResponseModel.class) {
            @Override
            public void onSuccess(Object model) {

            }

            @Override
            public void onErr(String errMsg) {

            }
        });
    }
}
